在动态列和行范围内复制值

时间:2015-08-19 16:58:11

标签: excel vba excel-vba

我有以下代码:

  • 将列标题(第1行)从C列复制到第二列
  • 将这些列标题粘贴到第2列中第1行的数据
  • 的最后一列
  • 将列标题与每行数据一起粘贴到底行

    Sub GLDR()
    
    'use End(xlUp) to determine Last Row with Data, in column A of the GLDRYYPP tab
    Dim lastRowDR As Long
    lastRowDR = Sheets("GLDRYYPP").Range("A" & Rows.Count).End(xlUp).Row
    
    'copy the cost type categories and paste alongside the cost centres
    CTNameCol = "S2:AF" & lastRowDR
    
    Sheets("GLDRYYPP").Range("C1", Range("C1").End(xlToRight).Offset(0, -1)).Copy
    Sheets("GLDRYYPP").Paste Destination:=Sheets("GLDRYYPP").Range("C1").End(xlToRight).Offset(0, 2)
    Sheets("GLDRYYPP").Range(Range("C1").End(xlToRight).Offset(0, 2), Range("C1").End(xlToRight).Offset(0, 2).End(xlToRight)).Copy
    
    Sheets("GLDRYYPP").Paste Destination:=Sheets("GLDRYYPP").Range(CTNameCol)
    
    End Sub
    

对于添加的任何其他列,前两个步骤已设置为动态,但我在编写将某些数据粘贴到底行的代码时遇到问题。如您所见,编写范围“S2:AF(最后一行)”是为了使用lastRowDR维度的结果。

有没有办法编写代码,使副本在列和行之间动态化?

1 个答案:

答案 0 :(得分:0)

是的,有一种方法可以编写代码,使副本在行和列之间动态化。您需要做的是确定有关成本中心的独特质量,以便您使用package util import org.specs2.matcher.Matchers import org.specs2.mutable.Specification class ElasticSearchSanitizerSpec extends Specification with Matchers { "sanitize" should { object S extends ElasticSearchSanitizer "escape special characters" in { S.sanitize("""back\slash""") mustEqual """back\\slash""" S.sanitize("""sl/ash""") mustEqual """sl\/ash""" S.sanitize("""pl+us""") mustEqual """pl\+us""" S.sanitize("""mi-nus""") mustEqual """mi\-nus""" S.sanitize("""amper&sand""") mustEqual """amper\&sand""" S.sanitize("""pi|pe""") mustEqual """pi\|pe""" S.sanitize("""ba!ng""") mustEqual """ba\!ng""" S.sanitize("""open(parenthesis""") mustEqual """open\(parenthesis""" S.sanitize("""close)parenthesis""") mustEqual """close\)parenthesis""" S.sanitize("""open{curly""") mustEqual """open\{curly""" S.sanitize("""close}curly""") mustEqual """close\}curly""" S.sanitize("""open[bracket""") mustEqual """open\[bracket""" S.sanitize("""close[bracket""") mustEqual """close\[bracket""" S.sanitize("""circum^flex""") mustEqual """circum\^flex""" S.sanitize("""til~de""") mustEqual """til\~de""" S.sanitize("""aste*risk""") mustEqual """aste\*risk""" S.sanitize("""ques?tion""") mustEqual """ques\?tion""" S.sanitize("""co:lon""") mustEqual """co\:lon""" } "escape set operators" in { S.sanitize("gin AND tonic") mustEqual """gin \A\N\D tonic""" S.sanitize("now OR never") mustEqual """now \O\R never""" S.sanitize("NOT never") mustEqual """\N\O\T never""" } "not escape set operators if part of words" in { S.sanitize("MANDATE") mustEqual "MANDATE" S.sanitize("NOTORIOUS") mustEqual "NOTORIOUS" } "not escape set operators if lowercase" in { S.sanitize("and or not") mustEqual "and or not" } "collapse excess whitespaces" in { S.sanitize("Y u no use single \t space??") mustEqual """Y u no use single space\?\?""" } "escape last quote if number of quotes is odd" in { S.sanitize("""Che "Guevarra" wears me" on his t shirt""") mustEqual """Che "Guevarra" wears me\" on his t shirt""" } "not escape any quotes if number of quotes even" in { S.sanitize("""Using these "lasers", we punch a hole in the "ozone layer"... """) mustEqual """Using these "lasers", we punch a hole in the "ozone layer"... """ } } } 之类的内容找到它。 然后,您将能够确定CTNameCol应该是什么。这里有一些有用的信息:Range.Find Method

如果你添加电子表格的图片,我会用更准确的信息更新这个答案。