473,395 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How Do you Loop through VBE Column cells?

103 100+
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

✓ answered by Rodney Roe

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").

1 2520
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
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...
1
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
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...
2
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...
5
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...
2
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...
2
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...
1
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
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 -...
4
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
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...

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.