[[20100318152124]] 『記述の方法』(yumie) ページの最後に飛ぶ

[ 初めての方へ | 一覧(最新更新順) | 全文検索 | 過去ログ ]

 

『記述の方法』(yumie)

こんにちは

マクロを始めたばかりで、以下の記述でしか出来ません。

他によい記述がありましたら教えて下さい。

       If Not Intersect(ActiveCell, Range("B6")) Is Nothing Then Me.Label4.Caption = Range("AD2").Value
       If Not Intersect(ActiveCell, Range("B10")) Is Nothing Then Me.Label4.Caption = Range("AF6").Value
       If Not Intersect(ActiveCell, Range("B14")) Is Nothing Then Me.Label4.Caption = Range("AD10").Value
       If Not Intersect(ActiveCell, Range("B18")) Is Nothing Then Me.Label4.Caption = Range("Z14").Value
       If Not Intersect(ActiveCell, Range("B22")) Is Nothing Then Me.Label4.Caption = Range("AD18").Value
       If Not Intersect(ActiveCell, Range("B27")) Is Nothing Then Me.Label4.Caption = Range("AF22").Value
       If Not Intersect(ActiveCell, Range("B31")) Is Nothing Then Me.Label4.Caption = Range("AD27").Value
       If Not Intersect(ActiveCell, Range("B35")) Is Nothing Then Me.Label4.Caption = Range("AD31").Value
       If Not Intersect(ActiveCell, Range("B39")) Is Nothing Then Me.Label4.Caption = Range("AD35").Value
       If Not Intersect(ActiveCell, Range("B43")) Is Nothing Then Me.Label4.Caption = Range("AF39").Value
       If Not Intersect(ActiveCell, Range("B47")) Is Nothing Then Me.Label4.Caption = Range("AD43").Value
       If Not Intersect(ActiveCell, Range("B52")) Is Nothing Then Me.Label4.Caption = Range("AD47").Value
       If Not Intersect(ActiveCell, Range("B56")) Is Nothing Then Me.Label4.Caption = Range("AD52").Value
       If Not Intersect(ActiveCell, Range("B60")) Is Nothing Then Me.Label4.Caption = Range("AF56").Value
       If Not Intersect(ActiveCell, Range("B64")) Is Nothing Then Me.Label4.Caption = Range("AD60").Value
       If Not Intersect(ActiveCell, Range("B68")) Is Nothing Then Me.Label4.Caption = Range("AF64").Value
       If Not Intersect(ActiveCell, Range("B72")) Is Nothing Then Me.Label4.Caption = Range("AD68").Value
       If Not Intersect(ActiveCell, Range("B77")) Is Nothing Then Me.Label4.Caption = Range("AD72").Value
       If Not Intersect(ActiveCell, Range("B81")) Is Nothing Then Me.Label4.Caption = Range("AD77").Value
       If Not Intersect(ActiveCell, Range("B85")) Is Nothing Then Me.Label4.Caption = Range("AF81").Value
       If Not Intersect(ActiveCell, Range("B89")) Is Nothing Then Me.Label4.Caption = Range("AD85").Value
       If Not Intersect(ActiveCell, Range("B93")) Is Nothing Then Me.Label4.Caption = Range("AD89").Value
       If Not Intersect(ActiveCell, Range("B97")) Is Nothing Then Me.Label4.Caption = Range("AD93").Value

xp 2003


 IF Then ... ElseIF でもいいし、
 Select Case でもいいし。

 Adst = ActiveCell.Address(0,0)
 IF Adst = "B6" THen
    CaoSt = Range("AD2").Value
 ElseIF Adst = "B10" Then
    CaoSt = Range("AF6").Value
    ・
  ・
 End if
 IF Capst <> "" Then
    Me.Label4.Caption = Capst
 End if

 //////
 dim st1 as variant, st2 as variant, ans as variant
 St1 = Array("B6","B10","B14",・・・・)
 St2 = Array("AD2","AF6","AD10",・・・・・

 Adst = ActiveCell.Address(0,0)
 ans = application.Match(Adst,St1,0)
 If not iserror(ans) Then
    Me.Label4.Caption = range(st2(ans)).value
 end if
 とか。
 BJ

 そのデータの拾い方をコード内でどうこうする前に
 元データと拾うデータのセルを2列の一覧表にするようにリンクを張って整理してから
 やってあげるとVlookupでもFindでも簡単に処理できるんだと思います。

 見栄え用のシートとデータのシートを分けて考える事をお勧めします。
 (momo)

 規則性を見出せればこんな塩梅に書く事もでけます。
      (弥太郎)
 '----------------------
    i = 2
    Do While i < 97
        i = i + 4
        Select Case i
            Case 10, 27, 43, 60, 68, 85
                adrs = "AF"
            Case 18
                adrs = "Z"
            Case Else
                adrs = "AD"
        End Select
        If Not Intersect(ActiveCell, Range("B" & i)) Is Nothing Then
            MsgBox "B" & i & "のCaptionは" & adrs & i - 4
            'Me.label4.Caption = Range(adrs & i - 4)
            Exit Do
        End If
        If i = 22 Or i = 47 Or i = 72 Then i = i + 1
    Loop


BJさん    スッキリしたコード有り難うございます。動きました。

momoさん アドバイス有り難うございます。

弥太郎さん  コード有り難うございます。

       B6:B25は、4行おきになっていますが

       次のB27は、5行目にあり、B27:B47は4行おきに

       そして、B52は、5行目にあり、B52:B72は4行おきに

       こんな感じの規則性なっています。

       B27、B52、B77の時にずれて表示されます。

       宜しくお願い致します。yumie


 Oh!アイムソリー!
 こんな塩梅に・・・

 MsgBox "B" & i & "のCaptionは" & adrs & i - IIf(i = 27 Or i = 52 Or i = 77, 5, 4)
 'Me.label4.Caption = Range(adrs & i - IIf(i = 27 Or i = 52 Or i = 77, 5, 4))
      (弥太郎)
  
      


弥太郎さん  DoとCaseの組み合わせ、勉強になります。

IIfは、はじめて見ました。使い方が、わからないので、調べてみます。

本当に有り難うございました。yumie


 >IIfは、はじめて見ました。使い方が、わからないので、調べてみます。
 因みにIIFは、ドンくさいです。
 BJ

コメント返信:

[ 一覧(最新更新順) ]


YukiWiki 1.6.7 Copyright (C) 2000,2001 by Hiroshi Yuki. Modified by kazu.