Excel内を検索して置換 Search and replace in Excel

こんにちは。
hi.

excel内の指定文字列をforeachでrow,column両方向に検索し、
文字列があればそれを置き換える方法を探しています。
Search the specified character string in excel in both row and column with foreach,
I want to know how to replace it if there is a string.

しかし、ReadCell とwriteCellのRange指定で引っかかってしまいます。
指定方法は下記リンク先を参考に作成しましたが、英語が苦手でよく読み込めていないです。
but, it will be stuck with the Range specification of ReadCell and writeCell.
I made it by referring to the following link destination, but I am poor at English and I can not read it well.
How to Write to Excel CELL index v.2

下記にサンプルをアップロードしましたので、
原因を教えていただけないでしょうか?
Since I uploaded the sample below,
Could you tell me the cause?
replace.xaml (19.4 KB)

よろしくお願いします。
best regards

1 Like

りよこさん、
こにちは

新しファイルを見てください。Replaceをつかてもじかわることができます。

もひとつはExcelをルプさせるためFor each row つかたほがいまつかてるFor each はちゅとちがいます。

よろしくお願いします。
replace.xaml (15.7 KB)

1 Like

Dear Nara

ありがとうございます!
アドバイス助かりました。
Thank!
Your advice was helpful.

For each rowを使用して目標の動作が行えました。
I was able to do the goal using the For each row activity.

Replaceですが、私の目的は変数tmpを置換ではなく
検索先セルの文字列を置換ですので、
教えていただいた方法は違うものだったようです。
認識間違っていたらごめんなさい。
For Replace, my purpose is to replace the variable tmp with the string of the destination cell instead of replacing it, so it seems that the method I taught was different.
Sorry if my perception was wrong.

replace_2.xaml (29.9 KB)

Naraさんへ追伸

日本語で回答くれて
ありがとうございました .+:。(´∀`)゚.+:。

英語でも書かないといけないかな、と思って
すごく緊張していたので、
日本語で回答もらえて嬉しかったです。

1 Like

りょくさん、こんにちは!

私は日本語が下手ですが答えてみます。

各Rowの各Cellをループするには、次のようにしてください:

  • For each row in DATATABLE (For each rowかTypeArgumentがDataRowにされたForEachのどちら)
  • この内で、For each item in row.ItemArray (For each, TypeArgument “Object”)
  • この内で、item = item.ToString.Replace(置換される文字列, 置換文字列) (Assignアクチビチ)

DataTableをExcelに戻いたいなら、ループあとでWrite Rangeを使ってください。

分からないことがあったらお知らせください。

よろしくお願いします!

Dear sfranzan

回答ありがとう!
日本語で返信もらえると、とても助かります。
Thanks for answering!
It is very helpful if you reply in Japanese.

教えて貰ったやり方で作成してみたのですが
WriteRangeで出力したDataTableが置換前のままなのです。
I tried to create it in the way I taught you
The DataTable output by WriteRange remains as it was before replacement.

どこを直せばよいのでしょうか?
ダウンロードフォルダに、この返信へ添付したexcelファイルを置いて実行しています。
Where should I fix it?
I place the excel file attached to this reply in the download folder and it is executed.

data.xls (25.5 KB)
replace_3.xaml (20.9 KB)

よろしくお願いします。
best regards

りょこさん、

ごめんなさい、私の先の回答でミスがあります。
row.ItemArrayしたらrowの内容の新しいインスタンスが生成されるので、rowとDataTable自体の内容が変更されません。
そのためには、row変数がインデックスでアクセスされてなければなりません:

For each index in Enumerable.Range(0, datatable.Columns.Count),
row(index) = row(index).ToString.Replace(...)

もちろん、If row(index).ToString = ..., row(index) = ...もいいです。実際に、文字列が完全一致の場合のみに置換されるべきであるの場合に、最高によい方法です。

もう一度ごめんなさい。

よろしくお願いします。

2 Likes

Dear sfranzan

出来ました!! :blush:
Completion!!

いただいたアドバイスとはちょっと違うのですが、
It is somewhat different from the advice I received,

For Each index in DATATABLE.columns
assign —> row(index.ToString)=row(index.ToString).ToString.Replace(tmp,replacetmp)

にしました。
row(index)ですとエラーが起きました…
I made it.
An error occurred when row (index) …

とても勉強になりました。
ifアクティビティでの置換も、挑戦してみようとおもいます。
I learned a lot.
I will try to replace with the if activity as well.

ありがとうございました!

data.xls (25.5 KB)
replace_4.xaml (20.6 KB)

りょこさん、

なるほど!私は知りませんでしたが、DataTableのColumnsプロパティが誠に使用されることがあります。
しかし、次のように使用してください:

For each column in DATATABLE.Columns:
Assign → row(column) = row(column).ToString.Replace(...)
か、
If row(column).ToString = 置換される文字列(か変数)、
Then Assign → row(column) = 置換文字列(か変数)

そのFor EachのTypeArgumentは「System.Data.DataColumn」でなければなりません。

じゃあ、今日は、私たちが二人とも新しいことを学んできたようですね。:smile:

よろしくお願いします!

1 Like

Dear sfranzan

教えていただいた通りに修正できました。
I was able to fix it exactly as I taught.

確かに、System.Data.DataColumnに設定しておけば
.tostringは不要ですね:smile:
surely, if you set it to System.Data.DataColumn for for each type
.tostring was unnecessary.

色々と教えていただいて感謝でいっぱいです。
私もいつかあなたのように誰かを助けてあげられるよう、頑張ります(^▽^*)
I am filled with appreciation for telling me various things.
I also want to be able to help someone like you some day (^ ▽ ^ *)

5 Likes

りょこさん、

どういたしまして!それに、温かい言葉をありがとう、嬉しいです。m(__)m

2 Likes

Hi ,
Here is the activities for Find and Find & Replace.

Regards
Balamurugan.S

1 Like