Selecting range based on another cell last row (VBA)

feni1388

Board Regular
Joined
Feb 19, 2018
Messages
133
Office Version
  1. 2021
Platform
  1. Windows
Hello....
Please help.....
I don't know what's wrong with this code.

Range("D6:G6").Select
Range("D6:G" & Range("F" & Rows.Count).End(xlUp).Row).Select


At this part, it ended up selecting A6:G39. Even though column F last row is F8.
There's no merge cells in all columns D to G.

Excel Formula:
Sub CopyData()

Sheets("Search").Select
Range("D6:G6").Select
Range("D6:G" & Range("F" & Rows.Count).End(xlUp).Row).Select


Sheets("NextPO").Select
Range("C" & Rows.Count).End(xlUp).Offset(1, 0).Select
Application.ScreenUpdating = True
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
Application.CutCopyMode = False

Sheets("Search").Select
    
End Sub

先日付確認用-test.xlsm
ABCDEFGH
3Delivery addressItemQtyCheck
4
5Search by Phone No -→DAB01165
6Company ADAB011651GL4310
72GL4410
83JT055
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
検索
Cell Formulas
RangeFormula
F5F5=IF(AND(C5<>"",D6<>""),"検索条件を修正してください",IF(C5<>"",VLOOKUP(SUBSTITUTE(TRIM(C5),"-",""),得意先及び直送先!U:V,2,FALSE),D6))
A6A6=IF(D6="","",VLOOKUP(D6,得意先及び直送先!A:K,2,FALSE)&VLOOKUP(D6,得意先及び直送先!A:F,3,FALSE))
Cells with Conditional Formatting
CellConditionCell FormatStop If True
F5Cell Valuecontains "検索"textNO
A6Cell Valuecontains "様"textNO
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
try

VBA Code:
Sheets("Search").Select
Range("D6").Select
Range("D6:F" & Cells(Rows.Count, "F").End(xlUp).Row).Select

..............................
 
Upvote 0
In the immediate window, paste this and hit enter. What does it return?
VBA Code:
Debug.Print Sheets("Search").Range("F" & Rows.Count).End(xlUp).Row
 
Upvote 0
Nothing comes up. But when I hover it on the Rows.Count part, it shows 1048576

I tried running it after selecting D6 cell.
but it stopped at Range("C" & Rows.Count).End(xlUp).Offset(1, 0).Select part
 
Upvote 0
Try So
Check and change references where required
Code:
Sub How_About_So()
Dim shS As Worksheet, shNP As Worksheet
Dim lr As Long
Set shs = Worksheets("Search")
Set shNP = Worksheets("NextPO")
lr = shS.Columns(6).Find("*", ,xlValues , , xlByRows, xlPrevious).Row
shNP.Cells(shNP.Rows.Count, 3).End(xlUp).Offset(1).Resize(lr - 5, 4).Value = shS.Cells(6, 4).Resize(lr - 5, 4).Value
End Sub
You have an overabundance of selecting which is normally frowned upon because of slowing down code.
 
Upvote 0
Do you mean?
Code:
    Dim x As Long
    With Sheets("search")
        With Intersect(.Columns("d:g"), .UsedRange)
            x = .Parent.Evaluate("max(if(" & .Address & "<>"""",row(" & .Address & ")))")
        End With
        If x Then
            .Range("d6:g" & x).Select
        Else
            MsgBox "No data to select"
        End If
    End With
 
Upvote 0
Try So
Check and change references where required
Code:
Sub How_About_So()
Dim shS As Worksheet, shNP As Worksheet
Dim lr As Long
Set shs = Worksheets("Search")
Set shNP = Worksheets("NextPO")
lr = shS.Columns(6).Find("*", ,xlValues , , xlByRows, xlPrevious).Row
shNP.Cells(shNP.Rows.Count, 3).End(xlUp).Offset(1).Resize(lr - 5, 4).Value = shS.Cells(6, 4).Resize(lr - 5, 4).Value
End Sub
You have an overabundance of selecting which is normally frowned upon because of slowing down code.
Your code is really close to what I expect to do.
But it ended up copying the total qty in column G (cell G33) and when I change the customer code, items and qty and run the macro again, it will paste it over the previous record (which has the last row on column F or G). I want the information to be pasted below the last used cell of Row "G", so the list can be continued.
 
Upvote 0
Do you mean?
Code:
    Dim x As Long
    With Sheets("search")
        With Intersect(.Columns("d:g"), .UsedRange)
            x = .Parent.Evaluate("max(if(" & .Address & "<>"""",row(" & .Address & ")))")
        End With
        If x Then
            .Range("d6:g" & x).Select
        Else
            MsgBox "No data to select"
        End If
    End With
I tried your code but it selects other columns too.
 
Upvote 0
This is to Select D6 to Gx, where x is the last row that have the value within col.D:F.
So, adjust for yourself, if needed.
Code:
    Dim x As Long
    With Sheets("search")
        With Intersect(.Columns("d:f"), .UsedRange)
            x = .Parent.Evaluate("max(if(" & .Address & "<>"""",row(" & .Address & ")))")
        End With
        If x Then
            .Range("d6:g" & x).Select
        Else
            MsgBox "No data to select"
        End If
    End With
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top