简体   繁体   English

Excel VBA-使用变量和COUNTA选择范围

[英]Excel VBA - Select a range using variables & COUNTA

Excel VBA - Select a range using variables & COUNTA Excel VBA-使用变量和COUNTA选择范围

Hi Staked VBA Kings & Queens, I'm trying to learn Excel VBA. 嗨,Staked VBA Kings&Queens,我正在尝试学习Excel VBA。 A simple task I would like to do is select all the contagious cells in a report dump I get from sales. 我想做的一个简单任务是选择我从销售中获得的报告转储中所有具有传染性的单元格。 Simple i'm sure, but I am a total beginner at VBA. 我敢肯定,但是我是VBA的初学者。

Ok Report Info: 确定报告信息:

The report is a set number of columns (31). 该报告是一组固定的列数(31)。 Although I would like to build a bit of variability into my code to accommodate a change in column numbers. 尽管我想在代码中加入一些可变性以适应列号的更改。

The report grows by number of rows each week, some times less, sometimes more. 该报告每周以行数增长,有时减少,有时增加。 But Always starts at cell [ A4 ]. 但始终从单元格[ A4 ]开始。

I though of using COUNTA function to count used number of rows, then set that as a variable. 我虽然使用COUNTA函数来计算使用的行数,然后将其设置为变量。 Similar with rows. 与行相似。

This is what I came up with, although I get a " Run-time Error '1004': Method 'Range' of object'_Global failed... can anyone help me out" . 这是我想出的,尽管我得到了“ 运行时错误'1004':对象'_Global的方法'Range'失败了……任何人都可以帮帮我”

For me the key is to learn VBA using task I need getting done. 对我来说,关键是要使用需要完成的任务来学习VBA。 I understand the logic behind my code, but not exactly the write way to write it. 我了解我的代码背后的逻辑,但不完全了解编写代码的方式。 If some proposes a totally different code I might get lost. 如果有人提出完全不同的代码,我可能会迷路。

But I am open minded. 但是我很开放。

Sub ReportArea()
        Dim numofrows As Integer
        Dim numofcols As Integer
        Dim mylastcell As String
        Dim myrange As Range

        Worksheets("Sheet1").Select
        numofrows = WorksheetFunction.CountA(Range("AE:AE"))
        numofcols = WorksheetFunction.CountA(Range("4:4"))
        Set myrange = Range(Cells(4, 1), Cells(numofrows, numofcols))
        Range(myrange).Select
End Sub

PS I did try read slimier trends but only got confused as the solution where very involved. PS我确实尝试读取更细的趋势,但在涉及非常广泛的解决方案时才感到困惑。

Find last row and last column 查找最后一行和最后一列

Sub Sht1Rng()
    Dim ws As Worksheet
    Dim numofrows As Long
    Dim numofcols As Long
    Dim myrange As Range
    Set ws = Sheets("Sheet1")
    With ws
        numofrows = .Cells(.Rows.Count, "AE").End(xlUp).Row
        numofcols = .Cells(4, .Columns.Count).End(xlToLeft).Column
        Set myrange = .Range(.Cells(4, 1), .Cells(numofrows, numofcols))
    End With
    MsgBox myrange.Address

End Sub

You can also use this code. 您也可以使用此代码。

Sub SelectLastCellInInSheet()
    Dim Rws As Long, Col As Integer, r As Range, fRng As Range
    Set r = Range("A1")
    Rws = Cells.Find(what:="*", after:=r, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Col = Cells.Find(what:="*", after:=r, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
    Set fRng = Range(Cells(2, 1), Cells(Rws, Col))    ' range A2 to last cell on sheet
    fRng.Select    'or whatever you want to do with the range
End Sub

Bookmark this page http://www.xlorate.com/selection-codes.html 将此页面添加为书签http://www.xlorate.com/selection-codes.html

Further to my above comment, is this what you are trying? 除了我上面的评论,这是您要尝试的吗?

Sub ReportArea()
    Dim ws As Worksheet
    Dim Lrow As Long
    Dim myrange As Range

    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        '~~> Find Last row of COl AE. Change it to the relevant column
        Lrow = .Range("AE" & .Rows.Count).End(xlUp).Row

        Set myrange = .Range("A4:AE" & Lrow)

        With myrange
            '
            '~~> Do whatever you want to do with the range
            '
        End With
    End With
End Sub

Note : Also you don't need to select a range/worksheet. 注意 :同样,您也不需要选择范围/工作表。 Work with objects. 处理对象。 Interesting Read 有趣的读物

also one additional option from my side to already posted variants\\ 从我这边来说,也是已经发布的变体的另一种选择\\

Sub test()
    Dim LRow&, LColumn
    Lrow = Sheets("Sheet1").Cells.SpecialCells(xlCellTypeLastCell).Row
    LColumn = Sheets("Sheet1").Cells.SpecialCells(xlCellTypeLastCell).Column
    MsgBox "Last Row is: " & Lrow & ", Last Column is: " & LColumn
End Sub

output result 输出结果

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM