473,609 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How Do you Loop through VBE Column cells?

103 New Member
Im trying to make my code (pasted below) a little more dynamic and effective. If I don't know how many columns are being used (maybe columns A:H is used or maybe A:AU is used), I want to loop through the columns as append the Column letter into an array. I might want to loop through A:H one time and later change to loop through columns H:AB. Can anyone help me with the logic or improve on what I am trying to accomplish?




Expand|Select|Wrap|Line Numbers
  1. Sub ProductCleanUp()
  2. '
  3. ' Macro1 Macro
  4. '
  5. Dim Col(1 To 20) As String
  6. Dim Row As Integer
  7. Row = 2
  8. Col(1) = "G"
  9. Col(2) = "H"
  10. Col(3) = "I"
  11. Col(4) = "J"
  12. Col(5) = "K"
  13. Col(6) = "L"
  14. Col(7) = "M"
  15. Col(8) = "N"
  16. Col(9) = "O"
  17. Col(10) = "P"
  18. Col(11) = "Q"
  19. Col(12) = "R"
  20. Col(13) = "S"
  21. Col(14) = "T"
  22. Col(15) = "U"
  23. Col(16) = "V"
  24. Col(17) = "W"
  25. Col(18) = "X"
  26. Col(19) = "Y"
  27. Col(20) = "Z"
  28.  
  29. Dim CurrentCell As String
  30. Dim PriorCell As String
  31. CurrentCell = "C" + CStr(Row)
  32. PriorCell = "C" + CStr(Row - 1)
  33.  
  34. While (Range(CurrentCell).Value <> "")
  35.         If (Range(CurrentCell).Value = Range(PriorCell).Value) Then
  36.             Dim rangeCopy As String
  37.             Dim rangePaste As String
  38.             Dim i As Integer
  39.                 For i = 1 To 20
  40.                 rangeCopy = Col(i) + CStr(Row)
  41.                 rangePaste = Col(i) + CStr(Row - 1)
  42.                     If (Range(rangeCopy).Value = "Yes") Then
  43.                         Range(rangeCopy).Copy Range(rangePaste)
  44.                     End If
  45.                 Next
  46.         Dim strDeleteRow As String
  47.         strDeleteRow = CStr(Row) + ":" + CStr(Row)
  48.         Rows(strDeleteRow).Select
  49.         Selection.Delete Shift:=xlUp
  50.         End If
  51.     Row = Row + 1
  52.     CurrentCell = "C" + CStr(Row)
  53.     PriorCell = "C" + CStr(Row - 1)
  54. Wend
  55.  
  56.  
  57. End Sub
  58.  
Jul 18 '12 #1
1 2529
Rodney Roe
61 New Member
Have you tried using range.offset()

Expand|Select|Wrap|Line Numbers
  1. for i = 0 to 19
  2.    [C1].offset(0,i)= [C1].offset(1,i)
  3. next
  4. ....
  5.  
something simmilar to that. [C1] is the shortcut version of range("C1").
Jul 23 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
24779
by: ssb | last post by:
Hi, As I understand, in MS-Access, the maximum length of data type "TEXT" is 255. Therefore, I am able to store a maximum of 255 characters only in the column cells. Is there a way to store longer text information in a column..? Thanks.
1
3642
by: nate axtell | last post by:
I'm trying to extend the Height of Windows.Forms Datagrid's column headers. Is it possible to allow wrapping of text in the header column cells along with making the height larger?
1
2642
by: jeguillo | last post by:
I am trying to retrieve the text within each cell in a datagrid in order to change the color of each cell, depending on the value within that cell. This works fine on the cells that are bound columns, but returns an empty string for the cells that are hyperlink columns. Here is the code to retrive the text: Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As
2
4952
by: dschectman | last post by:
This appears to be a feature of IE JavaScript. I am running IE 6.0 with the latest patches from Microsoft. Are there any workarounds other than re-coding the source HTML to place all the non-visible columns at the front? I have a page with a dynamic table. The table has some visible columns and some non-visible columns. The style "hide" is .hide { display:none; } <table id="SelectedList">
5
2275
by: bruxerjk | last post by:
I need to do the following. I want to copy cells from a number of Excel files, say "1.xls", "2.xls", "3.xls", etc, into successive rows of "All.xls". That is, I'd open "1.xls", copy cells and paste them into row 1 of "All.xls", then open "2.xls", copy cells and paste them into row 2 of "All.xls", and so on. I figure I can do this with some sort of For loop, but can't quite figure out how. What I need to know is a way of pointing to...
2
1971
by: Tony A. | last post by:
I'm trying to write a procedure that sums the values of a column in a datagridview. If a row is empty (has no value entered) the procedure crashes. I've added code to check to see if the column for the row is blank, this error apears: Conversion from type 'DBNull' to type 'Integer' is not valid. The code that I've written is: (the variables are defined) Do Until dgvTblService.CurrentCell.RowIndex = varRowCount
2
1815
by: amitsaxena1981 | last post by:
hi, I am using asp.net2.0 with vb.net , I have to create gridview user control and I want to drag and drop a record to a different position in the same Gridview using AJAX .Is it possible to implement this feature in gridview please help me its urgent and suggest the logic which i have to implement. Thanks & Regards Amit
1
1730
by: Emmanouil | last post by:
Goodmorning to every body!!!!! My "problem" for 2day is this: I have a column in a access database with 50 cells, let us say. All cells are filled with 1 and 0 quite like these: 1 0 0 1 1 0
0
2674
by: damiencarr | last post by:
i have a spreadsheet/worksheet called "NTH_Alliance" containing rows of related data grouped together under different headings, that are named as different weeks (e.g. Week 5 (26/01/2009 - 1/02/2009), or Week 6 (2/02/2009 - 8/02/2009)). im writing a macro where the user enters a specific week they wish to see data for (e.g. Week 5 (26/01/2009 - 1/02/2009)), and once the week is found (which can be on any row in the spreadsheet - i.e. not fixed...
4
2262
by: JonHuff | last post by:
here is my code and what I am trying to do - I want to start at row 2 and for each value in column B see if that cell value exists in range B10000:B10100. If it does exist, then go to the next row and evaluate, if it doesnt exist then highlight that cell adn go to the next row and evaluate. do this until column B is empty. it is highlighting all rows, even those where the cell value is in that defined range. HELP and Thank you in advance ...
0
8076
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8573
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8541
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8222
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8406
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7002
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5510
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1672
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.