下記のマクロをExcel95とExcel2000で動かすと結果(フォーマット)が違ってきます。
何処をどの様に修正すれば良いか教えてもらえないでしょうか。
言語の意味も出来たら教えてもらえますか。よろしくお願いします。
尚、省略している所があります。(言語は800文字以上は遅れませんので)
Sheets("印刷").Select
Dim gyo, cout, st, a, burank, st2, burank2
Dim total As Integer
Dim tt As String
cout = 1
Range("a3").Select
total = 2
Do
total = total + 1
burank = Worksheets("印刷").Cells(total, 25).Text
Loop While burank <> ""
total = total - 2
For a = 3 To total
If a = 3 Then
Range(Cells(a, 3), Cells(a, 26)).Select
With Selection.Borders(xlBottom)
.Weight = xlHairline
End With
Else
'
burank1 = Worksheets("印刷").Cells(a + 1, 2).Text
st = Worksheets("印刷").Cells(a, 1).Text
If st = "" And burank1 <> "" Then
'
Range(Cells(a, 2), Cells(a, 2)).Select
ActiveCell.Formula = "小 計"
'
With Selection
.HorizontalAlignment = xlCenter
End With
gyo = a + 1
Range(Cells(a, 1), Cells(a, 26)).Select
With Selection.Borders(xlLeft)
.Weight = xlThin
'
End With
No.1
- 回答日時:
>結果(フォーマット)が違ってきます
どのように違うかはEXCEL95を使用していないため、記憶に頼っていますが、罫線の設定で定数や実行結果に変更があったように思います(97の時?)。
EXCEL95の定数 xlLeft、xlBottom は、変更されて以下のようになっています。
xlEdgeLeft、xlEdgeBottom
(xlBordersIndexのメンバです)
この当たりの関係では?
No.2
- 回答日時:
コードの意味は『○』を付けた箇所に書いてみました。
修正箇所としては、<< ⇒ >> とした3箇所を修正してみてください。
実際、動かすことができないので見ただけの産物です。うまくいけばいいですが。
○シート印刷を選択
Sheets("印刷").Select
○配列の宣言。チャンと宣言したほうがいいか
Dim gyo, cout, st, a, burank, st2, burank2
Dim total As Integer
Dim tt As String
cout = 1
○A3を選択。多分、表の左上でしょう
Range("a3").Select
total = 2
○3行目から、データが入力された行数をカウントしている
Do
total = total + 1
burank = Worksheets("印刷").Cells(total, 25).Text
Loop While burank <> ""
○行数を余分に数えたので引いているが、これでは最終行-1の行数か。
total = total - 2
○3行目から最終行-1まで繰り返す
For a = 3 To total
○最初の行なら
If a = 3 Then
○3行目でA列から26列分を選択している。多分、表題か
Range(Cells(a, 3), Cells(a, 26)).Select
○選択した範囲の下罫線を極細にしている。<<xlBottom⇒xlEdgeBottom>>
With Selection.Borders(xlBottom)
.Weight = xlHairline
End With
Else
○4行目から、その行のA列と次の行のB列の入力具合を調べている
burank1 = Worksheets("印刷").Cells(a + 1, 2).Text
st = Worksheets("印刷").Cells(a, 1).Text
○その行のA列が未入力で次の行のB列に入力があった場合、以下の処理をする
If st = "" And burank1 <> "" Then
○その行のB列を選択して『小 計』の文字をセット
Range(Cells(a, 2), Cells(a, 2)).Select
ActiveCell.Formula = "小 計"
With Selection
○セットした文字をセルの中央に配置している。<<xlCenter⇒xlHAlignCenter>>
.HorizontalAlignment = xlCenter
End With
○変数gyoに+1
gyo = a + 1
○その行のA列から26列分を選択。多分、データ行
Range(Cells(a, 1), Cells(a, 26)).Select
○選択したセル範囲の左罫線を細線にしている。<<xlLeft⇒xlEdgeLeft>>
With Selection.Borders(xlLeft)
.Weight = xlThin
End With
この回答への補足
回答ありがとうございました。実は下記のコードの続きがあります。
良ければ意味を教えてください。
よろしくお願いします。
With Selection.Borders(xlEdgeBottom)
.Weight = xlMedium
End With
Range(Cells(a, 3), Cells(a, 4)).Select
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Range(Cells(a, 26), Cells(a, 26)).Select
ActiveCell.FormulaR1C1 = _
"=IF(RC[-12]>0,IF(AND(RC[-1]>0,RC[-1]<0.9),""●"",""""),"""")"
Else
If burank1 = "" Then
Range(Cells(a + 1, 1), Cells(a + 1, 26)).Select
With Selection.Borders(xlEdgeLeft)
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.Weight = xlMedium
End With
Range(Cells(a + 1, 3), Cells(a + 1, 4)).Select
Selection.Borders(xlLeft).LineStyle = xlNone
Range(Cells(a, 2), Cells(a, 2)).Select
ActiveCell.Formula = "小 計"
With Selection
.HorizontalAlignment = xlHAlignCenter
End With
Range(Cells(a, 1), Cells(a, 26)).Select
With Selection.Borders(xlEdgeLeft)
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.Weight = xlMedium
End With
Range(Cells(a, 3), Cells(a, 4)).Select
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Range(Cells(a, 26), Cells(a, 26)).Select
ActiveCell.FormulaR1C1 = _
"=IF(RC[-12]>0,IF(AND(RC[-1]>=0,RC[-1]<0.9),""●"",""""),"""")"
a = a + 2
Else
If a = gyo Then
Range(Cells(a, 3), Cells(a, 26)).Select
With Selection.Borders(xlEdgeBottom)
.Weight = xlHairline
End With
Else
'
Range(Cells(a, 1), Cells(a, 2)).Select
Selection.ClearContents
Range(Cells(a, 26), Cells(a, 26)).Select
ActiveCell.FormulaR1C1 = _
"=IF(RC[-12]>0,IF(AND(RC[-1]>=0.5,RC[-1]<=0.9),""☆"",IF(AND(RC[-1]>=0,RC[-1]<0.5),""★"","""")),"""")"
End If
End If
'アンダーライン(3:26)
Range(Cells(a, 3), Cells(a, 26)).Select
With Selection.Borders(xlEdgeBottom)
.Weight = xlHairline
End With
End If
End If
'アンダーライン(3:26)
Next a
Range(Cells(total + 1, 1), Cells(total + 1, 26)).Select
With Selection.Borders(xlEdgeLeft)
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.Weight = xlMedium
End With
Range(Cells(total + 1, 3), Cells(total + 1, 4)).Select
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Range("Y3").Select
ActiveCell.Formula = "=IF(n3=0,0,ROUND(x3/n3,1))"
Range("Y3").Select
Selection.Copy
Range(Cells(4, 25), Cells(total + 1, 25)).Select
Selection.PasteSpecial Paste:=xlFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
ActiveSheet.Calculate
Range("a1").Select
No.3ベストアンサー
- 回答日時:
修正漏れが1箇所?関係ないかもしれませんが追記しておきました。
○選択したセル範囲の下罫線を中太線(表現が正しい?)にしている。
With Selection.Borders(xlEdgeBottom)
.Weight = xlMedium
End With
○その行のC、D列を選択して左罫線をなしにしている
Range(Cells(a, 3), Cells(a, 4)).Select
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
○その行の26番目の列を選択して算式を登録している
算式は、その行の12個左の列が正の数値で、
1つ左のセルが0超、0.9未満なら『●』を表示する
Range(Cells(a, 26), Cells(a, 26)).Select
ActiveCell.FormulaR1C1 = _
"=IF(RC[-12]>0,IF(AND(RC[-1]>0,RC[-1]<0.9),""●"",""""),"""")"
Else
○1行下が未入力の場合
If burank1 = "" Then
○1行下のA列から26列分選択して左罫線を細線にしている
Range(Cells(a + 1, 1), Cells(a + 1, 26)).Select
With Selection.Borders(xlEdgeLeft)
.Weight = xlThin
End With
○下罫線を中太線にしている
With Selection.Borders(xlEdgeBottom)
.Weight = xlMedium
End With
○1行下のC、D列を選択して左罫線をなしにしている。<<xlLeft⇒xlEdgeLeft>>
Range(Cells(a + 1, 3), Cells(a + 1, 4)).Select
Selection.Borders(xlLeft).LineStyle = xlNone
○その行のB列を選択して『小 計』の文字をセット
Range(Cells(a, 2), Cells(a, 2)).Select
ActiveCell.Formula = "小 計"
○セットした文字をセルの中央に配置している。
With Selection
.HorizontalAlignment = xlHAlignCenter
End With
○その行のA列から26列分を選択
Range(Cells(a, 1), Cells(a, 26)).Select
○選択したセル範囲の左罫線を細線にしている。
With Selection.Borders(xlEdgeLeft)
.Weight = xlThin
End With
○選択したセル範囲の下罫線を中太線にしている。
With Selection.Borders(xlEdgeBottom)
.Weight = xlMedium
End With
○その行C、D列を選択して左罫線をなしにしている
Range(Cells(a, 3), Cells(a, 4)).Select
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
○その行の26番目の列を選択して算式を登録している。算式は同じ
Range(Cells(a, 26), Cells(a, 26)).Select
ActiveCell.FormulaR1C1 = "=IF(RC[-12]>0,IF(AND(RC[-1]>=0,RC[-1]<0.9),""●"",""""),"""")"
○Forループの中でそれを制御する変数aを操作している。恐い!。
本来やってはいけないはず!別の方法があるはず。
a = a + 2
Else
If a = gyo Then
Range(Cells(a, 3), Cells(a, 26)).Select
○下罫線を細線にしている。
With Selection.Borders(xlEdgeBottom)
.Weight = xlHairline
End With
Else
Range(Cells(a, 1), Cells(a, 2)).Select
Selection.ClearContents
Range(Cells(a, 26), Cells(a, 26)).Select
○算式をセットしている
算式は、その行の12個左の列が正の数値で、
1つ左のセルが0.5以上、0.9以下なら『☆』を表示する
1つ左のセルが0.5未満なら『★』を表示する
ActiveCell.FormulaR1C1 = "=IF(RC[-12]>0,IF(AND(RC[-1]>=0.5,RC[-1]<=0.9),""☆"",IF(AND(RC[-1]>=0,RC[-1]<0.5),""★"","""")),"""")"
End If
End If
'アンダーライン(3:26)
Range(Cells(a, 3), Cells(a, 26)).Select
With Selection.Borders(xlEdgeBottom)
○下罫線を細線にしている。
.Weight = xlHairline
End With
End If
End If
'アンダーライン(3:26)
Next a
○トータル行を選択
Range(Cells(total + 1, 1), Cells(total + 1, 26)).Select
With Selection.Borders(xlEdgeLeft)
○選択したセル範囲の左罫線を細線にしている。
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
○選択したセル範囲の下罫線を中太線にしている。
.Weight = xlMedium
End With
○その行C、D列を選択して左罫線をなしにしている
Range(Cells(total + 1, 3), Cells(total + 1, 4)).Select
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
○セルY3を選択して算式をセット
算式は、N3がゼロでなかったらX3をN3で割って、小数1位で四捨五入
Range("Y3").Select
ActiveCell.Formula = "=IF(n3=0,0,ROUND(x3/n3,1))"
Range("Y3").Select
Selection.Copy
○Y3の算式をデータの25列目にコピーしている
Range(Cells(4, 25), Cells(total + 1, 25)).Select
Selection.PasteSpecial Paste:=xlFormulas, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
○再計算している
ActiveSheet.Calculate
Range("a1").Select
※aでLoopしているForループの途中に『a = a + 2』がありますが問題なく動いてるんですよね。
全体を通して行っていることは、不定行の小計を計算して、それを罫線付の印刷で行おうとしているように思えます。
印刷用のシートにページ単位でデータをはきだし、小計行の罫線パターンを別に用意しておいて書式をコピーすれば簡単になりそうですね。
表示書式や割算の算式も事前に作っておけばマクロの中で定義する必要がなくなるわけです。
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
似たような質問が見つかりました
- Visual Basic(VBA) VBA 請求書自動作成 3 2022/04/24 01:58
- Visual Basic(VBA) Excelで下記のようにマクロを作ったところ、一回目は実行できたのですが、二回目以降「実行時エラー1 1 2022/03/25 08:08
- Visual Basic(VBA) VBA Userformで一部別シートに転記がしたいのですが 2 2023/05/24 13:08
- Visual Basic(VBA) ExcelVBAのマクロについて。 9 2022/05/04 14:50
- Excel(エクセル) 日付で矢印マクロ 4 2023/07/25 16:47
- Visual Basic(VBA) ExcelVBAでDo Until loopのネスト、IF文を使って一致する物と一致しない物としたい 11 2022/12/24 17:46
- Visual Basic(VBA) ローマ字、ハイフン付きの並び替え ローマ字抽出方法 Excelマクロ 4 2022/04/01 14:10
- Visual Basic(VBA) 前回ご教授いただいたコードに覚えたてのループ処理で品名りんごAから順に20回for nextでループ 7 2023/01/13 22:01
- Excel(エクセル) VBAで組み合わせ算出やCOUNTIFSの処理を高速化したいです。 4 2022/04/07 02:38
- Visual Basic(VBA) Sheet3から2つの条件でオートフィルターで抽出した個数をSheet2へ入力するマクロで、一つ目の 4 2023/01/12 23:40
関連するカテゴリからQ&Aを探す
おすすめ情報
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
Excelの外枠太罫線を2~3倍さ...
-
Excelの条件付き書式設定の太い...
-
勝手に背景に色が付いて困っ...
-
パワーポイントで、表の一部を...
-
excel2003 罫線だけを保護したい
-
決まった罫線のなかで、文章入...
-
エクセルで文字を入力すると罫...
-
EXCELで、下線の太さを変...
-
Excel:罫線を固定で残しておく...
-
斜め罫線の判定 ExcelVBA
-
罫線の色を薄くしたい
-
.NET上でエクセル上に罫線を引く
-
Excel カメラ機能でセル...
-
Excelに初期設定されている罫線...
-
罫線が引かれているセルの個数...
-
選択したセルに罫線を引くマクロ
-
エクセルの罫線を固定させる方...
-
EXCELのオートフィルタで罫線が...
-
OpenOfficeのCalcで罫線の削除...
-
Excelの条件書式の罫線の太さは...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
Excelの条件付き書式設定の太い...
-
Excelの外枠太罫線を2~3倍さ...
-
excel2003 罫線だけを保護したい
-
EXCELで、下線の太さを変...
-
罫線の色を薄くしたい
-
【エクセル】謎の枠線の消し方
-
エクセルで文字を入力すると罫...
-
エクセルのアンダーラインについて
-
エクセル
-
決まった罫線のなかで、文章入...
-
エクセルで罫線も一緒に並び替...
-
パワーポイントで、表の一部を...
-
エクセル 罫線入りの文字 文...
-
勝手に背景に色が付いて困っ...
-
罫線が引かれているセルの個数...
-
EXCELのオートフィルタで罫線が...
-
エクセル 入力されていない線...
-
罫線の色をまとめて変更する
-
エクセル表の罫線(縦)が消せ...
-
Excelでアンダーラインに文字が...
おすすめ情報