我正在尝试选择具有行号和列号作为变量的单元格块。让我们从一个静态的开始: -
我已经获得了合并单元格的行数。现在我尝试将用例的完整块复制到新工作表(与用例同名)。 例如,我试图将用例Random 1的范围(" C7:K11")复制到Worksheet Random1。
我遇到的唯一问题是使用varibale行号和列号复制此Range。 但是,此静态代码以及具有动态变量的代码不起作用: -
shtPricing.Range(Cells(7, 3), Cells(9, 11)).Copy
xWb.Worksheets(UsecaseTrail).Range(Cells(2, 3), Cells(4, 11)).PasteSpecial xlPasteValues
确切代码:
Dim lColumn As Long
Dim RowCount As Long
Dim ColumnCounter As Long
Dim RowCounter As Long
Dim tempUseCase As String
lColumn = xWb.Worksheets("Pricing").Cells(6, Columns.Count).End(xlToLeft).Column
For RowCounter = 7 To 25
RowCount = xWb.Worksheets("Pricing").Range("B" & RowCounter).MergeArea.Rows.Count
If RowCount > 1 Then
If InStr(1, CStr(xWb.Worksheets("Pricing").Range("B" & RowCounter).Value), UsecaseTrail) Then
xWb.Worksheets("Pricing").Range(Cells(RowCounter, 3), Cells(RowCounter + RowCount - 1, lColumn)).Copy
xWb.Worksheets(UsecaseTrail).Range(Cells(2, 3), Cells(2 + RowCount - 1, lColumn)).PasteSpecial xlPasteValues
End If
RowCounter = RowCounter + RowCount - 1 'note -1 here
End If
Next RowCounter
请注意,一切正常。除了具有变量Rowcounter和columncounter值的复制粘贴功能。 仅通过以下代码真的很有帮助: -
xWb.Worksheets("Pricing").Range(Cells(RowCounter, 3), Cells(RowCounter + RowCount - 1, lColumn)).Copy
xWb.Worksheets(UsecaseTrail).Range(Cells(2, 3), Cells(2 + RowCount - 1, lColumn)).PasteSpecial xlPasteValues
答案 0 :(得分:1)
考虑:
@available(iOS 11, *)
public extension Data {
public func jsonSerialized() -> Data? {
guard let json = try? JSONSerialization.jsonObject(with: self) else {
return nil
}
let object: Any = {
if let array = json as? Array<Any> {
return array.strippingNulls()
} else if let dictionary = json as? Dictionary<String, Any> {
return dictionary.strippingNulls()
} else {
return json
}
}()
guard let data = try? JSONSerialization.data(withJSONObject: object, options: [.sortedKeys, .prettyPrinted]) else {
return nil
}
return data
}
public static func jsonMismatch(lhs: Data, rhs: Data, alreadySerialized: Bool = false) -> Int? {
switch alreadySerialized {
case true:
return _jsonMismatch(lhs: lhs, rhs: rhs)
case false:
guard let lhs = lhs.jsonSerialized(), let rhs = rhs.jsonSerialized() else {
return nil
}
return _jsonMismatch(lhs: lhs, rhs: rhs)
}
}
private static func _jsonMismatch(lhs: Data, rhs: Data) -> Int? {
guard let string1 = String(data: lhs, encoding: .utf8), let string2 = String(data: rhs, encoding: .utf8) else {
return nil
}
let components1 = string1.components(separatedBy: "\n")
let components2 = string2.components(separatedBy: "\n")
let count = components1.count < components2.count ? components1.count : components2.count
for index in 0 ..< count {
if components1[index] != components2[index] {
return index
}
}
return nil
}
}
private extension Array where Element == Any {
func strippingNulls() -> Array {
var array = self
array.stripNulls()
return array
}
mutating func stripNulls() {
let count = self.count
guard count > 0 else {
return
}
for _index in 0 ..< count {
let index = count - 1 - _index
if self[index] is NSNull {
remove(at: index)
} else if let array = self[index] as? [Any] {
self[index] = array.strippingNulls()
} else if let dictionary = self[index] as? [String: Any] {
self[index] = dictionary.strippingNulls()
}
}
}
}
private extension Dictionary where Key == String, Value == Any {
func strippingNulls() -> Dictionary {
var dictionary = self
dictionary.stripNulls()
return dictionary
}
mutating func stripNulls() {
for (key, value) in self {
if value is NSNull {
removeValue(forKey: key)
} else if let array = value as? [Any] {
self[key] = array.strippingNulls()
} else if let dictionary = value as? [String: Any] {
self[key] = dictionary.strippingNulls()
}
}
}
}
修改#1:强>
这是一个经过测试的例子:
Dim r1 As Range, r2 As Range
With shtPricing
Set r1 = Range(.Cells(7, 3), .Cells(9, 11))
End With
With xWb.Worksheets(UsecaseTrail)
Set r2 = Range(.Cells(2, 3), .Cells(4, 11))
End With
r1.Copy
r2.PasteSpecial xlPasteValues
请注意Sub ytrewq()
Dim shtPricing As Worksheet
Dim r1 As Range, r2 As Range
Dim UsecaseTrail As String
Dim xWb As Workbook
Set shtPricing = Sheets("Sheet1")
Set xWb = ThisWorkbook
UsecaseTrail = "Sheet2"
With shtPricing
Set r1 = Range(.Cells(7, 3), .Cells(9, 11))
End With
With xWb.Worksheets(UsecaseTrail)
Set r2 = Range(.Cells(2, 3), .Cells(4, 11))
End With
r1.Copy
r2.PasteSpecial xlPasteValues
End Sub