This is a sample code. I am trying to get values of cells D9, E9 and F9
Private Sub test()
Dim myRange As Range
Dim lastrow As Long
Set ws1 = ThisWorkbook.Sheets("Sheet1")
lastrow = ws1.Range("A" & ws1.Rows.Count).End(xlUp).Row
Set myRange = ws1.Range("D9:F" & lastrow )
For Each cell In myRange
Debug.Print cell
Next cell
End Sub
But this cell also consist of values from column G and H. What am I missing?
Since you are trying to get the data for specific cells which you already know the addresses of (D9, E9, F9), you don't need to worry about getting the last row. Maybe you are trying to ask a different question? Maybe you want to get all of the data from row 9 to the last used used row for columns D, E, & F? Anyway, to answer the question asked, here is the modified code that will return the requested values:
Sub test()
Dim myRange As Range
Dim cell As Variant
Dim ws1 As Worksheet
Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set myRange = ws1.Range("D9:F9")
For Each cell In myRange
Debug.Print cell
Next cell
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.