需要修复VBA脚本中的运行时错误1004。

huangapple go评论67阅读模式
英文:

Need fix for runtime error 1004 in VBA_script

问题

I try to get my hands on VBA-scripts and wrote a script that results in a runtime error 1004

我尝试使用VBA脚本,并编写了一个导致运行时错误1004的脚本。

I wrote a script that should do the following:

  • check number of substring in each cell in column A; sheets DATA, whereby substrings are separated by "&"
  • if n>1: copy the row n-times below the respective cell; where n is number of substring in a cell
  • fill each cell with only one of the substrings

我编写了一个脚本,应该执行以下操作:

  • 检查列A中每个单元格中的子字符串数量;工作表为DATA,其中子字符串由"&"分隔
  • 如果n>1:复制行n次,位于相应单元格下方;其中n是单元格中的子字符串数量
  • 仅用一个子字符串填充每个单元格

According to the Editor I need to debug this following line below the second "&For-Block&":
Sheets(sheetName).Rows(i).Copy Destination:=Sheets(sheetName).Rows(i + 1)

根据编辑器的说法,我需要调试第二个"For-Block"下面的这行代码:
Sheets(sheetName).Rows(i).Copy Destination:=Sheets(sheetName).Rows(i + 1)

However, I don't see an error here.
Could anyone help?

然而,我在这里看不到错误。
有人可以帮忙吗?

英文:

I try to get my hands on VBA-scripts and wrote a script that results in a runtime error 1004

I wrote a script that should do the following:

  • check number of substring in each cell in column A; sheets DATA, whereby substrings are seperated by ";"
  • if n>1: copy the row n-times below the respective cell; where n is number of substring in a cell
  • fill each cell with only one of the substrings
Sub CopyRowsAndFillSubstrings()
    Dim lastRow As Long
    Dim i As Long, j As Long
    Dim substrCount As Long
    Dim substrArr() As String
    
    ' Specify the sheet name
    Dim sheetName As String
    sheetName = "Sheet1"
    
    ' Define the range of data
    lastRow = Sheets(sheetName).Cells(Rows.Count, "A").End(xlUp).Row
    
    ' Loop through each row
    For i = lastRow To 1 Step -1
        substrArr = Split(Sheets(sheetName).Range("A" & i).Value, ";")
        substrCount = UBound(substrArr) - LBound(substrArr) + 1
        
        If substrCount > 1 Then
            ' Copy the row n times, where n is the number of substrings
            For j = 2 To substrCount
                Sheets(sheetName).Rows(i + 1).Insert Shift:=xlDown
                Sheets(sheetName).Rows(i).Copy Destination:=Sheets(sheetName).Rows(i + 1)
            Next j
            
            ' Fill each cell in the copied rows with one of the substrings
            For j = 0 To substrCount - 1
                Sheets(sheetName).Cells(i + j, "A").Value = substrArr(j)
            Next j
        End If
    Next i
End Sub

According to the Editor I need to debug this following line below the second "For-Block":
Sheets(sheetName).Rows(i).Copy Destination:=Sheets(sheetName).Rows(i + 1)

However, I don't see an error here.
Could anyone help?

答案1

得分: 0

以下是您提供的代码的中文翻译:

根据您的需求,有两种解决方案。
Option Explicit

Sub CopyRowsAndFillSubstrings()
    Dim lastRow As Long
    Dim i As Long, j As Long
    'Dim substrCount As Long
    Dim substrArr() As String
    
    ' 指定工作表名称
    Dim wb As Workbook
    Dim ws As Worksheet
    Set wb = ActiveWorkbook
    Set ws = wb.Worksheets("Sheet1")
    
    ' 定义数据范围
    lastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row
    
    ' 循环遍历每一行
    For i = lastRow To 1 Step -1
        substrArr = Split(ws.Range("A" & i).Value, ";")
        'substrCount = UBound(substrArr) - LBound(substrArr) + 1
        '这与 UBound(substrArr) 相同
        
        '如果您想要替换";"字符串,请运行此代码块
        If UBound(substrArr) > 0 Then
            ' 复制该行 n 次,其中 n 是子字符串的数量
            ws.Rows(i + 1 & ":" & i + UBound(substrArr)).Insert shift:=xlDown
            For j = 0 To UBound(substrArr)
                ws.Cells(i + j, 1) = substrArr(j)
            Next j
        End If
        
        '或者如果您想要将它们写在";"字符串下方,请运行此代码块
'        If UBound(substrArr) > 0 Then
'            ' 复制该行 n 次,其中 n 是子字符串的数量
'            For j = 0 To UBound(substrArr)
'                ws.Rows(i + j + 1).Insert Shift:=xlDown
'                ws.Cells(i + j + 1, 1) = substrArr(j)
'            Next j
'        End If
    Next i
End Sub

如果您需要进一步的帮助,请告诉我。

英文:

There are two solutions depending on how you want it.

Option Explicit

Sub CopyRowsAndFillSubstrings()
    Dim lastRow As Long
    Dim i As Long, j As Long
    'Dim substrCount As Long
    Dim substrArr() As String
    
    ' Specify the sheet name
    Dim wb As Workbook
    Dim ws As Worksheet
    Set wb = ActiveWorkbook
    Set ws = wb.Worksheets("Sheet1")
    
    ' Define the range of data
    lastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row
    
    ' Loop through each row
    For i = lastRow To 1 Step -1
        substrArr = Split(ws.Range("A" & i).Value, ";")
        'substrCount = UBound(substrArr) - LBound(substrArr) + 1
        'It's the same as UBound(substrArr)
        
        'Run this if you want to replace the ";" string
        If UBound(substrArr) > 0 Then
            ' Copy the row n times, where n is the number of substrings
            ws.Rows(i + 1 & ":" & i + UBound(substrArr)).Insert shift:=xlDown
            For j = 0 To UBound(substrArr)
                ws.Cells(i + j, 1) = substrArr(j)
            Next j
        End If
        
        'Or run this if you want to write them under the ";" string
'        If UBound(substrArr) > 0 Then
'            ' Copy the row n times, where n is the number of substrings
'            For j = 0 To UBound(substrArr)
'                ws.Rows(i + j + 1).Insert Shift:=xlDown
'                ws.Cells(i + j + 1, 1) = substrArr(j)
'            Next j
'        End If
    Next i
End Sub

答案2

得分: 0

这条消息与您试图通过VBA更改的受保护单元格有关。假设您使用以下命令保护了工作表:

ASHEET.Protect Password:=YOUR_PASS, UserInterfaceOnly:=True, _
       AllowFormattingCells:=True, DrawingObjects:=False, Contents:=True

如果您保存工作簿,关闭它,然后重新打开它,当再次打开时,您必须使用相同的命令再次保护工作表,即使尝试使用VBA修改一些受保护的单元格也会出现错误消息。这就好像设置UserInterfaceOnly:=True没有被保存,每次打开工作簿时都需要提醒Excel。

Private Sub Workbook_Open()
   Call protectAllSheets
End Sub

Public Sub protectAllSheets()
   Dim r As Worksheet
   For Each r In ThisWorkbook.Worksheets
      r.Protect Password:=YOUR_PASS, _
                UserInterfaceOnly:=True, _
                AllowFormattingCells:=True, _
                DrawingObjects:=False, _
                Contents:=True
   Next
End Sub
英文:

This message is related to protected cells that you are trying to change through VBA. Suppose you protected the sheet with the command:

ASHEET.Protect Password:=YOUR_PASS, UserInterfaceOnly:=True, _
       AllowFormattingCells:=True, DrawingObjects:=False, Contents:=True

If you save the workbook, close it and then open it, YOU HAVE TO PROTECT THE SHEETS AGAIN WHEN OPENING with the same command, even when trying to modify with VBA some protected cells, you will get the error message. It's as if the setting UserInterfaceOnly:=True is not saved and you have to remind excel every time you open the book.

Private Sub Workbook_Open()
   Call protectAllSheets
End Sub

Public Sub protectAllSheets()
   Dim r As Worksheet
   For Each r In ThisWorkbook.Worksheets
      r.Protect Password:=YOUR_PASS, _
                UserInterfaceOnly:=True, _
                AllowFormattingCells:=True, _
                DrawingObjects:=False, _
                Contents:=True
   Next
End Sub

huangapple
  • 本文由 发表于 2023年6月29日 21:41:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76581608.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定