/ / 1st)列の識別2nd)列の値の識別3)その行のデータを配列に挿入します - excel、vba、excel-vba

1)列の識別2)列の値の識別3)その行のデータを配列に挿入します - excel、vba、excel-vba

水平データを収集しようとしています(左)セルI10から始めます - 最初に、列Iの正しい元帳勘定科目を識別するためにマクロをコーディングしようとしています。列Iの下にI:10が必要な列を表しています。 Dividend Income

Ledger Account
Prior Shares Outstanding
Current Shares Outstanding
Current Share Activity
Previous Net Assets
Current Net Assets
Net Asset Change
Market Value
Trading Gain Loss
Dividend Income

私は以下のコードを持っています、私は配列の中にI10(I:10からZ:10)の右にあるセルのデータを挿入しようとしています。

Sub Sample()
Dim ws As Worksheet
Dim MyAr As Variant
Dim SearchString As String
Dim aCell As Range
Dim i As Long

SearchString = "Ledger Account"

"~~> Change this as applicable
Set ws = ThisWorkbook.Sheets(1)

With ws
Set aCell = .Columns(1).Find(What:=SearchString, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)

If Not aCell Is Nothing Then
"~~> 5 Denotes the 5th column i.e Column "E"
"~~> Amend as applicable
"~~> Store the values From say Col B to E in the array
MyAr = Application.Transpose( _
.Range(.Cells(aCell.Row, 2), _
.Cells(aCell.Row, 5) _
).Value _
)

"~~> Check what is in the array
For i = LBound(MyAr) To UBound(MyAr)
Debug.Print MyAr(i, 1)
Next i
End If
End With
End Sub

回答:

回答№1は1

あなたの質問は紛らわしいと思います。

例1:何ですか I:10 to Z:10?どういう意味ですか I10 to Z10 または I10:Z10?コロンは、2つの単一セルアドレス間を行き来して範囲を定義します。

例2:テキストは、アカウント名のリストが列I(= 9)にあるが、コードが列1(= A)を検索していることを示しています。

この答えでは、私はあなたに正しい方向を示すことを願っています。


検討してください: Set ws = ThisWorkbook.Sheets(1).

ユーザーは複数のワークブックを開くことができます。それではA、B、C、Dと呼んでみましょう。ユーザはAをアクティブにできますが、Dでマクロを呼び出すことができます。 ActiveWorkbook アクティブなワークブックを参照する ThisWorkbook マクロを含むブックを参照します。 ユーザーが別のブックからマクロを呼び出す可能性があると思われる場合は、そのマクロを含むブックからワークシートが必要であることを明確に識別してください。さもないと Set ws = Sheets(1) アクティブワークブック内のワークシートをあなたに渡したでしょう。

避ける Sheets(1) または Sheets(2)。ここで1と2は、ユーザーが意図的にまたは誤って簡単に変更できるタブ行内の順序を表します。新しいブックを作成し、このコードをモジュールにコピーします。

Option Explicit
Sub Test1()

Dim InxWsht As Long

For InxWsht = 1 To Worksheets.Count
Debug.Print Worksheets(InxWsht).Name
Next

End Sub

デフォルトのシート数の英語版のExcelがある場合、このマクロはイミディエイトウィンドウに次の情報を出力します。

Sheet1
Sheet2
Sheet3

Sheet2をSheet3の後になるように移動し、マクロを再実行します。出力は次のとおりです。

Sheet1
Sheet3
Sheet2

ワークシートは必ず名前で識別してください。たとえば、ワークシート( "元帳")なので、ユーザーが誤ってマクロを壊すことはありません。


検討する Set aCell = .Columns(1).Find . . .

私が言ったようにこれは列1を検索しますこれは単なる誤植かもしれませんが、コード本体の列にリテラルを使用しないようにすることをお勧めします。新しい列が追加されるか、ユーザーが新しいシーケンスを希望すると、列が移動します。あなたがそれらを更新することができるようにどの番号が列を参照するかを識別しようとしているコードを通して捜すことは容易に避けられる悪夢かもしれません。

Const ColName As Long = 9

または

Const ColName As String = "I"

あなたが書くことができます

Set aCell = .Columns(ColName).Find . . .

元帳名を含む列が移動すると、一番上の1つの変更がコードを更新します。 “ ColName”が将来のメンテナンスプログラマーにとっては何かを意味するという利点もあります(あなたかもしれません)。作者が20の異なる列を番号で参照するようなマクロを更新しようとしたことがあれば、なぜ私が名前が好きなのか理解できます。


あなたの名前のリストをの列「A」にロードしましたワークシート「元帳」以下はあなたのマクロの修正版です。私の変化を研究し、なぜ私がそれらを作ったのか理解しようとします。必要に応じて質問して戻ってきますが、自分でできることが多ければ多いほど、早く成長するでしょう。

Sub Sample()

Const ColName As Long = 1
Const ColFirstWanted As Long = 2

Dim ws As Worksheet
Dim MyAr As Variant
Dim SearchString As String
Dim aCell As Range
Dim ColCrnt As Long   " Avoid names like i; they can get very confusing.
Dim ColLast As Long

SearchString = "Dividend Income"

"~~> Change this as applicable
Set ws = Worksheets("Ledgers")

With ws
Set aCell = .Columns(ColName).Find(What:=SearchString, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)

If Not aCell Is Nothing Then
" Find last used column in row
ColLast = .Cells(aCell.Row, Columns.Count).End(xlToLeft).Column

"~~> Amend as applicable
"~~> Store the values From say Col B to E in the array
"## Note: Transpose is slow when called from VBA.
MyAr = .Range(.Cells(aCell.Row, ColFirstWanted), .Cells(aCell.Row, ColLast)).Value

"~~> Check what is in the array
" ## Note: Although the worksheet row is not 1 and the first column is not 1,
" ##       the lower bounds are both 1. This can be confusing. I suggest
" ##       loading the entire row so the worksheet column numbers match the array
" ##       column numbers.  A few extra columns in the array is a small price to pay
" ##       reduced confusion.
For ColCrnt = LBound(MyAr, 2) To UBound(MyAr, 2)
Debug.Print ColCrnt & " " & MyAr(1, ColCrnt)
Next ColCrnt
End If
End With
End Sub