返回列表 上一主題 發帖

4個VBA問題

4個VBA問題

第1  sheet1    B1=2  C1=M   
dim n ,i
[D1]="Range("B"& n)   Range("C"& i)"  (因為 Range會變要這樣寫)

第2  好多時我都在sheet1 用按鈕run vba   1開始要在sheet2  我會先寫
sheets("sheet2").Select
但sheet2的名改了 "abc"  在vba又要改sheets("abc").Select
有什麼寫 改了sheet名都可以正常run vba.

第3
這樣寫對嗎?
sheets("sheet2").Range("A1").Value=sheets("sheet1").Range("A1").Value
如果改了sheet名都可以run要如何寫?

第4
這個很難><
sheet2

a1= 10 / 明/abc

當a1輸入1個數or文字   按run    if  a1的數or文字是第1次入的時候  c1會出1   vba會記載這個數or文字
                                             if  第2入就 c1出2 .....................

eg.   
a1=10 > run>c1=1   >   a1=momo>run>c1=1  >    save  execl off execl  >open excel >    a1=momo>run>c1=2  >a1=haha>run>c1=1

如果a1的數or文字有 300多個 vba可以save嗎?

VBA.rar (7.74 KB)

50 字節以內
不支持自定義 Discuz! 代碼

回復 15# kimbal


    謝謝大大^^
50 字節以內
不支持自定義 Discuz! 代碼

TOP

回復 13# basarasy


有關 "e1  and  f1  有問題"
可以把
    Dim es As String
的 as string 拿走就可以了

"如果我要把entries的東西save在 其他excel要如果寫?"
學習巨集基本法: 錄製巨集

在excel上
1. 開發人員->錄製巨集, 按確定
2. 常用->儲存格->格式->移動或複製工作表, 選位置
3. 開發人員->停止錄製
到vbe查看巨集
懂得發問,答案就會在其中

今日の一秒は  明日にない
http://kimbalko-chi.blogspot.com
http://kimbalko.blogspot.com

TOP

如果我要把entries的東西save在 其他excel要如果寫?
50 字節以內
不支持自定義 Discuz! 代碼

TOP

本帖最後由 basarasy 於 2010-7-25 17:05 編輯

回復 12# kimbal

大多都明白了.謝謝大大
只要把entries   del就可以 初期化了.

e1  and  f1  有問題
我入e1同f1 其實是  當我a1=10時 不小心按了2下run  c1出了2  之後入e1=10 f1=1 >run  c1就會
變回1.  不用把e1同f1 save在entries,只要改entries內 對照的b:b 就ok了.

不save是否改
If es = "" Then
            .Range("A1").Offset(currRow - 1) = v
            End If

If es = "" Then        
        .Range("A1").Offset(currRow - 1, 1) = nextvalue
        End If  這個?

但改entries內 對照的b:b不知改那裡.
50 字節以內
不支持自定義 Discuz! 代碼

TOP

回復 11# basarasy
有關c1問題, 之次錯刪了一下,可以再試試這個
  1.     On Error Resume Next
  2.     Dim currRow As Long
  3.     Dim es As String
  4.     Dim e As Integer
  5.     Dim nextvalue As Integer
  6.     Dim sheetname As String
  7.    
  8.     sheetname = Worksheets("entries").Name
  9.    
  10.     If sheetname = "" Then
  11.         MsgBox ("Sheet ""entries"" not found!")
  12.         Exit Sub
  13.     End If
  14.     currRow = 0
  15.    
  16.     v = Range("A1")
  17.    
  18.     es = Range("E1").Value
  19.     e = Range("F1").Value
  20.     If es <> "" Then
  21.         v = es
  22.     Else
  23.         e = 0
  24.     End If
  25.    
  26.     currRow = WorksheetFunction.Match(v, Worksheets(sheetname).Range("A:A"), False)
  27.     With Worksheets(sheetname)
  28.         If currRow = 0 Then
  29.             currRow = .Range("A65536").End(xlUp).Row + 1
  30.             .Range("A1").Offset(currRow - 1) = v
  31.             nextvalue = 1
  32.         Else
  33.             nextvalue = .Range("A1").Offset(currRow - 1, 1) + 1
  34.         End If
  35.         If e > 0 Then
  36.             nextvalue = e
  37.         End If
  38.         .Range("A1").Offset(currRow - 1, 1) = nextvalue
  39.     End With
  40.    
  41.     If currRow > 0 Then
  42.         Range("C1").Value = nextvalue
  43.     End If
複製代碼
這個或者你試試自己動手吧,
"可以加多個g1   if  g1=1 就把 所有save的東西del"
save的代碼是
thisworkbook.save

"還有,如果我這個excel有問題,"
可以,vba內容跟沒有跟workbook定位.... 你也試試了解一下中關運作吧~有行數不明白可以問一下的.
懂得發問,答案就會在其中

今日の一秒は  明日にない
http://kimbalko-chi.blogspot.com
http://kimbalko.blogspot.com

TOP

回復 10# kimbal
RUN時有問題
A1=10  E1="">RUN 2次>  C1出1(正常C1會出2 )
18-21.那裡是要   IF  ES<>"" AND V=ES  THEN....>RUN >C1=e
可以加多個g1   if  g1=1 就把 所有save的東西del. 還有,如果我這個excel有問題,可以把save的東西copy到第2個excel嗎?
50 字節以內
不支持自定義 Discuz! 代碼

TOP

本帖最後由 kimbal 於 2010-7-25 14:13 編輯

回復 9# basarasy

有關"有少少問題"...
  1. On Error Resume Next
  2.     Dim currRow As Long
  3.     Dim es As String
  4.     Dim e As Integer
  5.     Dim nextvalue As Integer
  6.     Dim sheetname As String
  7.     sheetname = Worksheets("entries").Name
  8.    
  9.     If sheetname = "" Then
  10.         MsgBox ("Sheet ""entries"" not found!")
  11.         Exit Sub
  12.     End If
  13.     currRow = 0
  14.    
  15.     v = Range("A1").Value
  16.     es = Range("E1").Value
  17.     e = Range("F1").Value
  18.     If es <> "" Then
  19.         v = es
  20.     Else
  21.         e = 0
  22.     End If
  23.    
  24.     currRow = WorksheetFunction.Match(v, Worksheets(sheetname).Range("A:A"), False)
  25.     With Worksheets(sheetname)
  26.         If currRow = 0 Then
  27.             currRow = .Range("A65536").End(xlUp).Row + 1
  28.             nextvalue = 1
  29.         Else
  30.             nextvalue = .Range("A1").Offset(currRow - 1, 1) + 1
  31.         End If
  32.         If e > 0 Then
  33.             nextvalue = e
  34.         End If
  35.         .Range("A1").Offset(currRow - 1, 1) = nextvalue
  36.     End With
  37.    
  38.     If currRow > 0 Then
  39.         Range("C1").Value = nextvalue
  40.     End If
複製代碼
Workbooks(AA).Close SaveChanges:=False
Close argument 下的filename 是用來指定close後把excel存成新的檔名,
而不是用來指定關那個book
懂得發問,答案就會在其中

今日の一秒は  明日にない
http://kimbalko-chi.blogspot.com
http://kimbalko.blogspot.com

TOP

回復 8# kimbal


    有少少問題
我想要  if  e1=""   就正常run ,  then   if  e1=a1  f1=2(e1和f1都是人手打的) then c1出2 else end
eg.

a1=30  e1="" > run > c1=1 > run > c1=2 > e1=30 f1=1 > run > c1=f1=1 > e1="" > run > c1=2

請問
AA = "C:\Documents and Settings\Administrator\桌面\book.xlsm"

Workbooks.Open Filename:=AA  (這個可以run)

Workbooks.Close Filename:=AA , savechanges:=True  (為什麼這個不能run?)

test.rar (15.38 KB)

50 字節以內
不支持自定義 Discuz! 代碼

TOP

本帖最後由 kimbal 於 2010-7-25 09:58 編輯

回復 7# basarasy


    有關4的問題, 或者換個方法吧
用前不要忘了加 entries一頁
  1.     On Error Resume Next
  2.     Dim currRow As Long
  3.     Dim e As Integer
  4.     Dim nextvalue As Integer
  5.     Dim sheetname As String
  6.     sheetname = Worksheets("entries").Name
  7.    
  8.     If sheetname = "" Then
  9.         MsgBox ("Sheet ""entries"" not found!")
  10.         Exit Sub
  11.     End If
  12.     currRow = 0
  13.     v = Range("A1").Value
  14.     e = Range("E1").Value
  15.    
  16.     currRow = WorksheetFunction.Match(v, Worksheets(sheetname).Range("A:A"), False)
  17.     With Worksheets(sheetname)
  18.         If currRow = 0 Then
  19.             currRow = .Range("A65536").End(xlUp).Row + 1
  20.             nextvalue = 1
  21.         Else
  22.             nextvalue = .Range("A1").Offset(currRow - 1, 1) + 1
  23.         End If
  24.         If e > 0 Then
  25.             nextvalue = e
  26.         End If
  27.         .Range("A1").Offset(currRow - 1, 1) = nextvalue
  28.     End With
  29.    
  30.     If currRow > 0 Then
  31.         Range("C1").Value = nextvalue
  32.     End If
複製代碼
有關forumla的問題:
  1. Range("AA3").Formula = "=IF(" & MyName & "!C2="""","""",VLOOKUP(A2,poi!$A$2C$501,3,0))"
複製代碼
懂得發問,答案就會在其中

今日の一秒は  明日にない
http://kimbalko-chi.blogspot.com
http://kimbalko.blogspot.com

TOP

        靜思自在 : 地上種了菜,就不易長草;心中有善,就不易生惡。
返回列表 上一主題