简体   繁体   English

似乎试图获得相同范围时得到不同的结果

[英]Getting different results when seemingly trying to get the same range

I'm having a problem which could also be a bug, either way, I don't know how to resolve it and would appreciate any help :) 我遇到一个问题,也可能是一个错误,无论哪种方式,我都不知道如何解决,并且希望得到任何帮助:)

i2 = 2
plz1 = Worksheets("Sheet2").Range("B" & i2)
plz2 = Worksheets("Sheet2").Range("C" & i2)

B2 is 89999 and C2 is 90000. So when running this code plz1 is 89999 and plz 2 is "90000" (notice the quotation marks). B2是89999,C2是90000。因此,运行此代码时,plz1是89999,plz 2是“ 90000”(请注意引号)。

So: 所以:

plz1 = 89999
plz2 = "90000"

This is quite an issue since I want to create a range: 这是一个很大的问题,因为我想创建一个范围:

Range(plz1 & ":" & plz2)

I don't know if I am just doing a really obvious stupid mistake or if it's a bug... 我不知道我是在做一个非常明显的愚蠢错误还是一个错误...

I would really appreciate any advice I can get! 我真的很感激我能得到的任何建议! :)) :))

Thanks, NiceRice 谢谢,NiceRice

Maybe you could try this instead? 也许您可以尝试一下呢?

Sub Combined_range()
i2 = 2
Sheets("Sheet2").Range ("B" & i2 & ":C" & i2)
End Sub

Not sure if you mean to construct a range of numbers or a range of rows. 不确定要构造数字范围还是行范围。

Here is how you can deal with either a text 90000 or "90000" in a cell. 这是在单元格中处理文本90000或“ 90000”的方法。 Also, then have a testNumber and see if it is between plz1 and plz2. 另外,还有一个testNumber,看看它是否在plz1和plz2之间。

Option Explicit
Sub test()

Dim i2 As Long
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet2") 'Change as appropriate

Dim plz1 As Long
Dim plz2 As Long

i2 = 2

'Get rid of any "" if present in string

If InStr(1, ws.Range("C" & i2), Chr(34)) > 0 Then
    ws.Range("C" & i2) = Application.WorksheetFunction.Substitute(ws.Range("C" & i2), Chr(34), "")
End If

On Error GoTo NotConverted

plz1 = CLng(ws.Range("B" & i2))
plz2 = CLng(ws.Range("C" & i2))

Dim myRange As Range
Set myRange = ws.Range(plz1 & ":" & plz2)

MsgBox myRange.Address

Dim testNumber As Long
testNumber = 1238

If testNumber >= plz1 And testNumber <= plz2 Then
    MsgBox "testNumber is between " & plz1 & " and " & plz2
End If

Exit Sub

NotConverted:
MsgBox "The contents of either cell B" & i2 & " or C" & i2 & " could not be converted to a number."

End Sub

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

相关问题 尝试选择范围时获得1004 - Getting 1004 when trying to select a range 尝试从另一个打开的工作簿中的某个范围获取值时,出现运行时错误9:下标超出范围 - getting Run-time Error 9: Subscript out of Range when trying to get value from a range in another open workbook 尝试在相同行但不同列的范围上设置边框 - Trying to set borders on range with same rows but different columns 相同的公式,不同的结果? - Same formula, different results? 尝试获取列中的条目数时下标超出范围 - Subscript out of range when trying to get number of entries in column 获取运行时 1004:尝试清除命名范围时 object '_Worksheet' 的方法 'Range' 失败 - Getting run-time 1004: Method 'Range' of object '_Worksheet' failed when trying to clear out a named range 与笔记本电脑相比,在 Surface 平板电脑上运行时获得不同的结果 - Getting different results when running on Surface tablet compared to laptop WorksheetFunction.CountA 在同一个工作表上使用时返回不同的结果 - WorksheetFunction.CountA returns different results when used on the same worksheet 尝试消除VBA中的.activate和.select,尝试设置范围时出现错误1004 - Trying to eliminate .activate and .select in VBA, getting Error 1004 instead when trying to SET range 尝试更改范围时,行索引范围不起作用 - Range of RowIndex not Working when trying to change the range
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM