Я намагаюся отримати кодування VBA, яке буде шукати комірки, відформатовані як дата, і для всіх форматів дат знову шукати певний текст над ним і скопіювати його в рядок, для якого є цифрова дата.
Для ілюстрації нижче наведено зразок моїх даних, щоЯ хочу надіслати адресу. Для будь-якої комірки у стовпці C, яка форматована як дата, я сподіваюсь, що код знайде номер клієнта (клітина C2) і ім'я клієнта (D2) і скопіюватиме до стовпців A та B на відповідний рядок. Проблема виникає тому, що багато клієнтів у моїх даних мають більше ніж один ваучер, тому я не можу натискати його, щоб просто бути двома рядами над коміркою, яка має дату. Я сподіваюсь, що це буде сенс!
Customer account Name
1001010 Internal : Sales Admin (9900)
Date Voucher
12/7/2011 CINV00000980
1/26/2012 CINV00001011
2/9/2012 CINV00001050
3/6/2012 CINV00002003
3/13/2012 CINV00002067
Я намагаюся використовувати вкладений ланцюжок "Позиції" прямо зараз (див. Нижче), і, очевидно, це не працює. Код працює, але не відбувається копіювання / вставлення.
Sub ARAging()
Dim row As Integer
row = 2
finalrow = ActiveSheet.UsedRange.Rows.Count
Do
If Range(Cells(row, 3), Cells(row, 3)).NumberFormat = "dd/mm/yyyy" Then
Do
If Range(Cells(row, 3), Cells(row, 3)).Value = "Customer account" Then
Range(Cells(row - 1, 3), Cells(row - 1, 4)).Select
With Selection.Copy
End With
End If
row = row - 1
Loop Until Range(Cells(row, 3), Cells(row, 3)).Value = "Customer account"
Range(Cells(row, 1), Cells(row, 2)).Select
With Selection.Paste
End With
End If
row = row + 1
Loop Until row = finalrow
End Sub
Будь-яка допомога буде вдячна!
Відповіді:
2 для відповіді № 1Я думаю, цей код робить те, що ви хочете. Я сподіваюсь, що я включив достатньо коментарів, щоб пояснити мій код. Якщо потрібно, поверніться з питаннями.
" Option Explicit means every variable must be explicitly declared. Without
" it a misspelt variable name becomes an implicit declaration. This can be
" a very difficult error to find.
Option Explicit
Sub ARAging()
" On a 32-bit system, the native size is what Excel calls Long. So integer
" variables are slower than long variables.
" My style is to start a variable name with its type and then add classifiers
" to identify its purpose. This means that when I return to this macro in
" six or twelve months, I know what the variables are. If you do not like my
" style, develop your own or develop one with colleagues. Consistent names
" are a godsend during maintenence.
Dim acctNumCust As String
Dim rowCrnt As Long
Dim rowFinal As Long
With Worksheets("Sheet1")
" I avoid ActiveSheet unless I want the user to be able to run the macro
" against a worksheet of their choice.
acctNumCust = ""
rowFinal = .UsedRange.Rows.Count
For rowCrnt = 2 To rowFinal
" In .Cells(R,C), C can be a number or a column code.
" If I am accessing a single cell, I do not need to make it into
" a range since .Cells(R,C) is a range.
If .Cells(rowCrnt, "C").Value = "Customer account" Then
" Since I know column C is "Customer account", I do not need to
" save it. Save the value of column 4 in a variable not the
" scratchpad.
acctNumCust = .Cells(rowCrnt, "D").Value
" I do not like testing for the formatting. What if the user changes
" it to dd-mmm-yy?
ElseIf IsDate(.Cells(rowCrnt, "C").Value) Then
.Cells(rowCrnt, "A").Value = "Customer account"
.Cells(rowCrnt, "B").Value = acctNumCust
Else
Call MsgBox("Cell C" & rowCrnt & " is neither """ & _
"Customer account"" nor a date", vbOKOnly)
End If
Next rowCrnt
End With " Worksheets("Sheet1")
End Sub