Board logo

標題: [發問] VBA 自動插入至空白格方式 [打印本頁]

作者: lichang    時間: 2015-10-30 22:43     標題: VBA 自動插入至空白格方式

本帖最後由 lichang 於 2015-10-30 22:45 編輯

請問各位大大,我製作了一個表單,想要讓客戶可以自行加入新馬達
我該如何撰寫,讓客戶填寫完成的資料後,自動插入到空白的填空格,(如C1,C2,C3格),或者排到E1,E2,E3
然後再按照馬達規格大小排列順序??
如下圖
[attach]22275[/attach]

[attach]22276[/attach]
作者: lichang    時間: 2015-11-2 22:14

自頂一下!!麻煩大大悶的幫忙!!
作者: owen06    時間: 2015-11-3 15:54

回復 2# lichang


    你如果能夠敘述的更明確一點,甚或舉出完整的例子來,大家會比較好幫忙你,
    因為你說明的太少了,讓人摸不著頭緒,不知道該如何幫忙。
作者: lichang    時間: 2015-11-4 00:19

不好意思,可能之前檔案有問題,我重新放置檔案與用圖示方式解釋好了
[attach]22334[/attach]  原始資料
[attach]22331[/attach]  開啟對話視窗,並輸入資料
[attach]22335[/attach]  查詢有空格,填入資料
[attach]22336[/attach]  因馬達編號大小不一,重新安排一次!!結束


[attach]22337[/attach]
作者: lpk187    時間: 2015-11-4 06:05

回復 4# lichang

試試!
  1. Private Sub CommandButton1_Click()
  2. Dim Col As Integer
  3. Col = Sheets("工作表1").[a1].End(xlToRight).Column + 1
  4. Cells(1, Col) = TextBox1.Value
  5. Cells(2, Col) = TextBox2.Value
  6. Cells(3, Col) = TextBox3.Value
  7. End Sub
複製代碼

作者: owen06    時間: 2015-11-4 09:48

回復 4# lichang


    幫補自動排序
  1. Private Sub CommandButton1_Click()
  2. Dim Col As Integer
  3. Col = Sheets("工作表1").[a1].End(xlToRight).Column + 1
  4. Cells(1, Col) = TextBox1.Value
  5. Cells(2, Col) = TextBox2.Value
  6. Cells(3, Col) = TextBox3.Value
  7. Range(Cells(1, 2), Cells(3, [XFD3].End(1).Column)).Select
  8. Selection.Sort Key1:=Range("B1"), Order1:=xlAscending
  9. End Sub
複製代碼

作者: lichang    時間: 2015-11-4 10:28

謝謝各位大大幫忙,我測試一下
作者: owen06    時間: 2015-11-4 11:03

回復 7# lichang


    對了,如果你的office是2003的,那
    Range(Cells(1, 2), Cells(3, [XFD3].End(1).Column)).Select
    要改成
    Range(Cells(1, 2), Cells(3, [iv3].End(1).Column)).Select
    才不會超出範圍
作者: lichang    時間: 2015-11-4 11:35

回復  lichang


    幫補自動排序
owen06 發表於 2015-11-4 09:48



大大,我測試後,排列順序發生問題,因為他是把直排整個重新排列,但是左右順序沒有變化
我是希望左右排列順序要更換,排列優先是馬達規格、編號、驅動器
作者: 准提部林    時間: 2015-11-4 12:10

Private Sub CommandButton1_Click()
Dim C&, i&
C = Cells(1, Columns.Count).End(xlToLeft).Column + 1
For i = 1 To 3: Cells(i, C) = UserForm1("TextBox" & i): Next
Range([B1], Cells(3, C)).Sort Key1:=[B1], Order1:=xlAscending, Header:=xlNo, _
   OrderCustom:=1, MatchCase:=False, Orientation:=xlLeftToRight
End Sub
作者: owen06    時間: 2015-11-4 13:03

回復 9# lichang


    sorry,改這樣應該就行了
  1. Private Sub CommandButton1_Click()
  2. Dim Col As Integer
  3. Col = Sheets("工作表1").[a1].End(xlToRight).Column + 1
  4. Cells(1, Col) = TextBox1.Value
  5. Cells(2, Col) = TextBox2.Value
  6. Cells(3, Col) = TextBox3.Value
  7. Range(Cells(1, 2), Cells(3, [iv3].End(1).Column)).Select
  8. Selection.Sort Key1:=Range("b1"), Order1:=xlAscending, Header:=xlGuess, _
  9.         OrderCustom:=1, MatchCase:=False, Orientation:=xlLeftToRight, SortMethod _
  10.         :=xlStroke, DataOption1:=xlSortNormal
  11. End Sub
複製代碼

作者: lichang    時間: 2015-11-4 22:40

回復 10# 准提部林

版主語法真是簡潔有力,不愧是板主
作者: lichang    時間: 2015-11-5 09:11

回復 10# 准提部林

大大請問一下,如果我是從別的分頁來呼叫該分頁時,前面要怎麼加,因為我現在呼叫時,他是以我目前呼叫的分頁來做排列
而不是指定分頁
作者: 准提部林    時間: 2015-11-5 09:49

回復 13# lichang


Private Sub CommandButton1_Click()
Dim C&, i&
With Sheets("Sheet1") 
   C = .Cells(1, Columns.Count).End(xlToLeft).Column + 1
   For i = 1 To 3: .Cells(i, C) = UserForm1("TextBox" & i): Next
   Range(.[B1], .Cells(3, C)).Sort Key1:=.[B1], Order1:=xlAscending, Header:=xlNo, _
     OrderCustom:=1, MatchCase:=False, Orientation:=xlLeftToRight
End With
End Sub

注意紅色的 . 不可漏掉∼∼
作者: lichang    時間: 2015-11-5 22:48

回復 14# 准提部林

大大,小弟真的很不才,想拿您的程式碼來修改,但是一直沒辦法了解真諦
怎麼改都一直發生錯誤,您方便跟我說一下怎麼改嗎??
以下是我修改的程式碼

With Sheets("工作表2")
      C = .Cells(10, Columns.Count).End(xlToLeft).Column + 1
      Range(.[C10], .Cells(15, C)).Sort Key1:=.[C10], Order1:=xlAscending, Header:=xlNo, _
          OrderCustom:=1, MatchCase:=False, Orientation:=xlLeftToRight


[attach]22358[/attach]

[attach]22359[/attach]
作者: 准提部林    時間: 2015-11-6 09:35

回復 15# lichang


Sub TEST()
Dim C&
With Sheets("工作表2")
   C = .Cells(10, Columns.Count).End(xlToLeft).Column + 1
   Range(.[C10], .Cells(15, C)).Sort Key1:=.[C10], Order1:=xlAscending, Header:=xlNo, _
     OrderCustom:=1, MatchCase:=False, Orientation:=xlLeftToRight
End With
End Sub

以上測試OK,不過我是OFFICE 2000,
您可使用錄製方式取得〔橫向排序〕程式碼,再套至 Sort 那行看看∼∼
作者: lichang    時間: 2015-11-6 17:22

我測試過了,我發現是放在模組中使用就可以了,但是我做再表單中,就有問題了!!!!




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)