简体   繁体   English

VBA为ActiveSheet而不是A1:B2设置范围

[英]VBA set range for ActiveSheet instead of A1:B2

I'm a noob in VBA I have data range from Row1 to Row 226 , trying to export it as csv file in comma delimited. 我是VBA的菜鸟,我的数据范围是从Row1到第226行 ,尝试将其导出为以逗号分隔的csv文件。

Sub Comma()

Dim r As Range: Set r = Range("A1:D4") 
Dim buffer As String, delimiter As String, c As Range
Dim i As Long

I want to set the Range to be the current sheet "Sheet1" 我想将范围设置为当前工作表“ Sheet1”

When I do Dim r As Range: Set r = Range("Sheet1") , it's throwing out me an error and I can't seem to find what I desire on SO. 当我做Dim r As Range: Set r = Range("Sheet1") ,它抛出了一个错误,我似乎找不到我想要的东西。

Can anyone please help me? 谁能帮帮我吗? Thank you 谢谢

You need to find the last row and then construct your range. 您需要找到最后一行,然后构造范围。 See This link 看到这个链接

Sub Sample()
    Dim ws As Worksheet
    Dim lastrow As Long
    Dim rng As Range

    Set ws = Sheets("Sheet1")

    With ws
        If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
            lastrow = .Cells.Find(What:="*", _
                          After:=.Range("A1"), _
                          Lookat:=xlPart, _
                          LookIn:=xlFormulas, _
                          SearchOrder:=xlByRows, _
                          SearchDirection:=xlPrevious, _
                          MatchCase:=False).Row
        Else
            lastrow = 1
        End If

        Set rng = .Range("A1:D" & lastrow)
    End With
End Sub

and in case your last column is also not fixed then use this 如果您的最后一列也没有固定,请使用此

Sub Sample()
    Dim ws As Worksheet
    Dim lastrow As Long, lastCol As Long
    Dim rng As Range
    Dim colName As String

    Set ws = Sheets("Sheet1")

    With ws
        If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
            lastrow = .Cells.Find(What:="*", _
                          After:=.Range("A1"), _
                          Lookat:=xlPart, _
                          LookIn:=xlFormulas, _
                          SearchOrder:=xlByRows, _
                          SearchDirection:=xlPrevious, _
                          MatchCase:=False).Row

            lastCol = .Cells.Find(What:="*", _
                          After:=.Range("A1"), _
                          Lookat:=xlPart, _
                          LookIn:=xlFormulas, _
                          SearchOrder:=xlByColumns, _
                          SearchDirection:=xlPrevious, _
                          MatchCase:=False).Column
        Else
            lastrow = 1: lastCol = 1
        End If

        colName = Split(Cells(, lastCol).Address, "$")(1)

        Set rng = .Range("A1:" & colName & lastrow)
    End With
End Sub

Try the code below, as good practice always use Option Explicit at the top of your module. 尝试下面的代码,因为好的做法总是在模块顶部使用Option Explicit

Option Explicit

Sub Comma()

Dim r As Range
Dim LastCell As Range
Dim LastRow As Long

Dim buffer As String, delimiter As String, c As Range
Dim i As Long

With Worksheets("Sheet1")
    ' get dynamic last row
    Set LastCell = .Cells.Find(What:="*", After:=Cells(1), Lookat:=xlPart, LookIn:=xlFormulas, _
                            SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False)
    If Not LastCell Is Nothing Then
        LastRow = LastCell.Row ' get last Row with data
    End If

    Set r = Worksheets("Sheet1").Range("A1:D" & LastRow) '<-- set Range to "Sheet1"
End With


End Sub

尝试:

Dim r As Range: Set r = Sheets("Sheet1").Range("A1:D4")

暂无
暂无

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

相关问题 PyXll:用 L1C1 代替 A1:B2 的范围 - PyXll : Range with L1C1 instead A1:B2 IF公式仅在A1 = A2,B1 = B2时返回值 - IF Formula to return value only if A1=A2, B1=B2 Excel:如果A1匹配A2,则从B1自动填充B2 - Excel: Autofill B2 from B1 if A1 matchs A2 预期:由 Sheets(&quot;PrepaymentCustomer&quot;).Range(&quot;B2:B4&quot;) 结束的语句 (VBA) - Expected: end of statement (VBA) by Sheets("PrepaymentCustomer").Range("B2:B4") 如果 A2 不等于 A1、B1 不等于 B2,则突出显示 A1,依此类推 - Highlight A1 if A2 does not equal A1, B1 does not equal B2, and so on 将“ A1,A2,A3 ..”添加到“ B1,B2,B3 ..”,然后行“ A”将值重置为零 - Adding “A1,A2,A3..” to “B1,B2,B3..” Then Row “A” resets value to Zero 每次单击按钮时,VBA 中是否有一种简单的方法可以将 Range(“B2:B5, FB2:OR5”) 移动超过 1? - Is there an easy way in VBA to shift the Range(“B2:B5, FB2:OR5”) over 1 each time you click a button? 当 A1 和 B1 匹配 A2 和 B2 时,尝试连接 C 列中的值 - Trying to concatenate values in Column C when A1 & B1 match A2 & B2 如果A1 = A2,并且B1&gt; B2,则隐藏第2行。要使用哪些公式/规则以及在哪里? - If A1 = A2, and B1>B2 then hide Row 2. What formulas/rules to use and where? 如果A1发生变化,则将一些内容放入B1 | 如果A2发生变化,请在B2中放入一些内容 - If A1 changes, put something into B1 | If A2 changes, put something into B2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM